﻿using DotNet5.Mvc.Common;
using DotNet5.Mvc.Context;
using FreeSql.Internal;
using System;
using System.Diagnostics;

namespace DotNet5.Mvc.FreeSqlBuilder
{
    public static class FreeSqlFactory
    {
        public static IFreeSql CreateFreeSql()
        {
            IFreeSql fsql = null;
            try
            {
                fsql = CallContext<IFreeSql>.GetData("FreeSql");
                if (fsql == null)
                {
                    string freeSqlConnection = AppSettingsProvider.connectionStrings.FreeSqlConnection;
                    fsql = new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.ShenTong, freeSqlConnection)
                    .UseMonitorCommand(cmd => Trace.WriteLine($"线程：{cmd.CommandText}\r\n"))
                    .UseAutoSyncStructure(false) // true：自动创建、迁移实体表结构
                    .UseNameConvert(NameConvertType.ToUpper)
                    .Build();
                    CallContext<IFreeSql>.SetData("FreeSql", fsql);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("连接异常："+ex.Message);
                throw ex;
            }
            return fsql;
        }
    }
}
